package ysu.sxm.ptjc.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.AccountInfoDao;
import ysu.sxm.ptjc.Dao.ProRelationInfoDao;
import ysu.sxm.ptjc.Dao.produceInfoDao;
import ysu.sxm.ptjc.Dao.ProduceStatisticalInfoDao;
import ysu.sxm.ptjc.Domin.Daily;
import ysu.sxm.ptjc.Service.DailyStatisticsService;
import ysu.sxm.ptjc.pojo.AccountInfo;
import ysu.sxm.ptjc.pojo.ProRelationInfo;
import ysu.sxm.ptjc.pojo.ProduceInfo;
import ysu.sxm.ptjc.pojo.ProduceStatisticalInfo;

import java.sql.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@Service
public class DailyStatisticsServiceImpl implements DailyStatisticsService {

    @Autowired
    ProduceStatisticalInfoDao produceStatisticalInfoDao;
    @Autowired
    AccountInfoDao accountInfoDao;
    @Autowired
    produceInfoDao produceInfoDao;
    @Autowired
    ProRelationInfoDao proRelationInfoDao;

    @Override
    public List<Daily> getAllByDate(Date d){
        List<ProduceStatisticalInfo> list;
        list = produceStatisticalInfoDao.getProduceStatisticalInfoByDate(d);
        List<Daily> dailies = geInfos(list);
        return dailies;
    }
    @Override
    public List<Daily> geInfos (List<ProduceStatisticalInfo> produceStatisticalInfos){
        List<Daily> dailies = new LinkedList<>();
        Daily d = new Daily();
        Iterator iter = produceStatisticalInfos.iterator();
        while(iter.hasNext()){
            ProduceStatisticalInfo produceStatisticalInfo = (ProduceStatisticalInfo)iter.next();
            d.setProduceNum(produceStatisticalInfo.getProduceNum());
            d.setProduceDate(produceStatisticalInfo.getProduceDate());
            d.setInfoId(produceStatisticalInfo.getInfoId());
            d.setReworkNum(produceStatisticalInfo.getReworkNum());
            d.setScrapNum(produceStatisticalInfo.getScrapNum());
            d.setProduceQuantity(produceStatisticalInfo.getProduceQuantity());
            d.setQualifyNum(produceStatisticalInfo.getQualifyNum());

            ProRelationInfo proRelationInfo = proRelationInfoDao.getProRelationInfoByProduceNum(produceStatisticalInfo.getProduceNum());
            d.setCustomerId(proRelationInfo.getCustomerId());
            d.setColor(proRelationInfo.getColor());
            d.setCarTypeId(proRelationInfo.getCarTypeId());
            d.setWorkshopId(proRelationInfo.getWorkshopId());

            AccountInfo accountInfo = accountInfoDao.findAccountById(produceStatisticalInfo.getInfoId());
            d.setReworkRate(accountInfo.getReworkRate());
            d.setScrapRate(accountInfo.getScrapRate());
            d.setUnqualified_Rate(accountInfo.getUnqualified_Rate());
            d.setFirstpassRate(accountInfo.getFirstpassRate());
            d.setTotalqualified_Rate(accountInfo.getTotalqualified_Rate());

            List<ProduceInfo> produceInfos = produceInfoDao.getProduceInfoByNumAndDate(new Date(produceStatisticalInfo.getProduceDate().getTime()),produceStatisticalInfo.getProduceNum());
            Iterator iter2 = produceInfos.iterator();
            while(iter2.hasNext()){
                ProduceInfo produceInfo = (ProduceInfo)iter2.next();
                d.setProduceId(produceInfo.getProduceId());
                d.setProduceType(produceInfo.getProduceType());
                d.setLineId(produceInfo.getLineId());
                d.setGroup(produceInfo.getGroup());
                d.setProduceName(produceInfo.getProduceName());
                dailies.add(d);
            }
        }
        return dailies;
    }
}
